package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StateEnum;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.Novel;
import cn.cnf.entity.NovelDetails;
import cn.cnf.service.ChapterService;
import cn.cnf.service.NovelDetailsService;
import cn.cnf.service.NovelService;
import cn.cnf.service.SubscribeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.istack.internal.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/novel")
public class NovelController {
    @Autowired
    private NovelService novelService;
    @Autowired
    private NovelDetailsService novelDetailsService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private SubscribeService subscribeService;

    /**
     * 新增&更新小说
     *
     * @param novel
     * @return
     */
    @PostMapping("/addOrUpdateNovel")
    public Result addComic(@RequestBody Novel novel) {
        try {
            return Result.success(novelService.addOrUpdate(novel));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @PostMapping("/addOrUpdateDetails")
    public Result addOrNovelDetils(@RequestBody NovelDetails novelDetails) {
        try {
            return Result.success(novelService.addOrNovelDetails(novelDetails));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }



    @GetMapping("/details")
    public Result getChapterDetails(@RequestParam("novelId") String novelId, @RequestParam("chapterId") String chapterId) {
        try {
            // 如果没有订阅
            if (!subscribeService.authority(UserRequestUtil.getCurrentUserName(), novelId, chapterId,WorkType.NOVEL)) {
                return Result.warn("您没有订阅，请前往订阅或充值");
            }
            return Result.success(novelService.getChapterDetails(novelId, chapterId));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/deleteNovelChapter")
    public Result deleteChapter(@RequestParam("novelId") String novelId, @RequestParam("chapterId") String chapterId) {
        try {
            novelDetailsService.delete(novelId, chapterId);
            chapterService.removeById(chapterId);
            return Result.success("删除成功");
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/deleteNovel")
    public Result deleteChapter(@RequestParam("novelId") String novelId) {
        try {
            novelDetailsService.delete(novelId);
            chapterService.delete(novelId, WorkType.NOVEL);
            novelService.removeById(novelId);
            return Result.success("删除成功");
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/personalNovel")
    public Result works(String username) {
        try {
            return Result.success(novelService.getPersonalComics(StringUtils.isBlank(username) ? UserRequestUtil.getCurrentUserName() : username));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/updateState")
    public Result updateComicState(@RequestParam("novelId")String novelId
            ,@RequestParam("state")Integer state) {
        try {
            return Result.success(novelService.updateState(novelId, StateEnum.getByValue(state)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/listNovel")
    public Result listNovel(@NotNull @RequestParam("novelId")String novelId){
        try {
            LambdaQueryWrapper<Novel>  wrapper = Wrappers.lambdaQuery();
            wrapper.eq(Novel::getId,novelId);
            return Result.success(novelService.getOne(wrapper));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }
}